<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="../error.jsp"%>

<%@page import="java.sql.*"%>

<%@page import="df.jsp.Guard"%>
<%@page import="df.jsp.LabelMap"%>
<%@page import="df.bean.db.conn.DBConnection"%>
<%@page import="df.bean.db.table.TRN_Error"%>
<%@page import="df.bean.obj.util.JDate"%>
<%@page import="df.bean.db.table.Batch"%>
<%@page import="df.bean.process.ProcessUtil"%>
<%@ include file="../../_global.jsp" %>
<%@page import="df.bean.obj.util.Utils "%>
<%
            //
            // Verify permission
            //

            if (!Guard.checkPermission(session, Guard.PAGE_PROCESS_DEMO)) {
                response.sendRedirect("../message.jsp");
                return;
            }

            //
            // Initial LabelMap
            //

            if (session.getAttribute("LANG_CODE") == null) {
                session.setAttribute("LANG_CODE", LabelMap.LANG_EN);
            }
            LabelMap labelMap = new LabelMap(session.getAttribute("LANG_CODE").toString());
            labelMap.add("TITLE_MAIN", "Process All Monthly", "Process All Monthly");
            labelMap.add("COL_0", "Process", "Process");
            labelMap.add("COL_1", "Status", "Status");
            labelMap.add("DATE", "Date", "Date");;
            labelMap.add("PROGRESS", "Progress", "Progress");
            
            request.setAttribute("labelMap", labelMap.getHashMap());
            String hospitalCode = session.getAttribute("HOSPITAL_CODE").toString();
            String user = session.getAttribute("USER_ID").toString();
            String startDateStr = JDate.showDate(JDate.getDate());

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>${labelMap.TITLE_MAIN}</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" type="text/css" href="../../css/share.css" media="all" />        
        <link rel="stylesheet" type="text/css" href="../../css/default/easyui.css" />
        <link rel="stylesheet" type="text/css" href="../../css/calendar.css" />
        <script type="text/javascript" src="../../javascript/util.js"></script>
        <script type="text/javascript" src="../../javascript/ajax.js"></script>
        <script type="text/javascript" src="../../javascript/calendar.js"></script>        
        <script type="text/javascript" src="../../javascript/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="../../javascript/jquery.easyui.min.js"></script>
        <script type="text/javascript">
    	var doctorData = new Object();
		var objData = new Object();
		var num = 0;
    	var num2 = 0;
    	var result = false;
    	var xhr;
		
        function serResult(resultBoolean){
        	result = resultBoolean;
        }
        (function($) {
	        var ajaxQueue = $({});
	        $.ajaxQueue = function( ajaxOpts ) {
	            var jqXHR,
	                dfd = $.Deferred(),
	                promise = dfd.promise();
	            ajaxQueue.queue( doRequest );
	            promise.abort = function( statusText ) {
	                if ( jqXHR ) {
	                    return jqXHR.abort( statusText );
	                }
	                var queue = ajaxQueue.queue(),
	                    index = $.inArray( doRequest, queue );

	                if ( index > -1 ) {
	                    queue.splice( index, 1 );
	                }
	                dfd.rejectWith( ajaxOpts.context || ajaxOpts,
	                    [ promise, statusText, "" ] );

	                return promise;
	            };
	            function doRequest( next ) {
	                jqXHR = $.ajax( ajaxOpts )
	                    .then( next, next )
	                    .done( dfd.resolve )
	                    .fail( dfd.reject );
	            }

	            return promise;
	        };
	        })(jQuery);
	        function getDoctorData(){
	        	var hospitalCode = $("#hospitalCode").val();
				$.ajax({
					type: "GET",
					url: "../../ProcessAllMonthlySrvl",
					data: {mode:"getDoctor", hospitalCode:hospitalCode},
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					async : false,
					cache : false,
					success: function(data){
							doctorData = data;
					},
					error: function(xhr,state,exception) {
						doctorData="";
					}
				});
				objData = doctorData;
	        }
        $(document).ready(function() {
            alert("ready");
        	//var date = new Date();
        	//var d  = date.getDate();
        	//var m = date.getMonth() + 1;
        	//var month = (m < 10) ? '0' + m : m;
        	//var yy = date.getYear();
        	//document.getElementById('date').value = d+"/"+month+"/"+yy;
        	document.getElementById("progress_monthly").innerHTML = "0/0";
        	document.getElementById("progress_expense").innerHTML = "0/2";
        	document.getElementById("progress_payment").innerHTML = "0/0";
        	document.getElementById("progress_bank").innerHTML = "0/1";            
			function monthlyCalulate(){
	        	var i=0;
	        	num=0;
	        	for(i=0;i<objData.listDoctorCode.length;i++){
					xhr = $.ajaxQueue({
							type: "GET",
							url: "../../ProcessAllMonthlySrvl",
							data: {mode:"processMonthly",date:$("#date").val(),doctorCode : objData.listDoctorCode[i].doctorCode,count:i,hospitalCode : $("#hospitalCode").val(),
								   maxSize : objData.listDoctorCode.length },
							contentType: "application/json; charset=utf-8",
							dataType: "json",
							cache : false,
							success: function(data){
								if(num==0){
						        	document.getElementById("img_monthly_calculate").src="../../images/processing_icon.gif";
								}
								document.getElementById("progress_monthly").innerHTML = data.count+" / " + objData.listDoctorCode.length;
								if(data.lastData == "Y"){
									if(data.count>0){
										document.getElementById("img_monthly_calculate").src="../../images/succeed_icon.gif";
										if(data.count == objData.listDoctorCode.length){
											expenseCalulate();
										}else{
											document.getElementById("img_monthly_calculate").src="../../images/failed_icon.gif";
											document.getElementById("img_expense_calculate").src="../../images/failed_icon.gif";
											document.getElementById("img_payment_summary").src="../../images/failed_icon.gif";
											document.getElementById("img_bank_payment").src="../../images/failed_icon.gif";
											document.getElementById("RUN").disabled = "";
											document.getElementById("STOP").disabled = "";
								        	alert("    Fail!!!  ");
										}
									}else{
										document.getElementById("img_monthly_calculate").src="../../images/failed_icon.gif";
										document.getElementById("img_expense_calculate").src="../../images/failed_icon.gif";
										document.getElementById("img_payment_summary").src="../../images/failed_icon.gif";
										document.getElementById("img_bank_payment").src="../../images/failed_icon.gif";
										document.getElementById("RUN").disabled = "";
										document.getElementById("STOP").disabled = "";
							        	alert("    Fail!!!  ");
									}
								}
								num++;
							},
							error: function(xhr,state,exception) {
								alert("exception monthlyCalulate => " + exception);
							}
					    }); 
	        	}
			}
			function expenseCalulate(){
				var buffer=0;
				xhr = $.ajaxQueue({
						type: "GET",
						url: "../../ProcessAllMonthlySrvl",
						data: {mode:"expenseCalulate",date:$("#date").val(),hospitalCode : $("#hospitalCode").val(),
						processType : "Remove Expense Transaction",count:buffer},
						contentType: "application/json; charset=utf-8",
						dataType: "json",
						async : false,
						cache : false,
						success: function(data){
							document.getElementById("img_expense_calculate").src="../../images/processing_icon.gif";
							serResult(false);
							if(data.count>0){
								document.getElementById("progress_expense").innerHTML = data.count+" / 2";
							}
						},
						error: function(xhr,state,exception) {
							alert("exception expenseCalulate => " + exception);
						}
				    }); 
        		buffer++;
        		xhr = $.ajaxQueue({
					type: "GET",
					url: "../../ProcessAllMonthlySrvl",
					data: {mode:"expenseCalulate",date:$("#date").val(),hospitalCode : $("#hospitalCode").val(),
					processType : "Summary Expense Transaction",count:buffer},
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					async : false,
					cache : false,
					success: function(data){
						if(data.count>0){
							document.getElementById("progress_expense").innerHTML = data.count+" / 2";
							if(data.count == 2){
					        	document.getElementById("img_expense_calculate").src="../../images/succeed_icon.gif";
					        	paymentSummary();
							}else{
					        	document.getElementById("img_expense_calculate").src="../../images/failed_icon.gif";
								document.getElementById("img_payment_summary").src="../../images/failed_icon.gif";
								document.getElementById("img_bank_payment").src="../../images/failed_icon.gif";
								document.getElementById("RUN").disabled = "";
								document.getElementById("STOP").disabled = "";
					        	alert("    Fail!!!  ");
							}
						}else{
				        	document.getElementById("img_expense_calculate").src="../../images/failed_icon.gif";
							document.getElementById("img_payment_summary").src="../../images/failed_icon.gif";
							document.getElementById("img_bank_payment").src="../../images/failed_icon.gif";
							document.getElementById("RUN").disabled = "";
							document.getElementById("STOP").disabled = "";
				        	alert("    Fail!!!  ");
						}
					},
					error: function(xhr,state,exception) {
						alert("exception => " + exception);
					}
			    }); 
			}
			function paymentSummary(){
	        	var i=0;
	        	document.getElementById("img_payment_summary").src="../../images/processing_icon.gif";	
				for(i=0;i<objData.listDoctorCode.length;i++){
					xhr = $.ajaxQueue({
							type: "GET",
							url: "../../ProcessAllMonthlySrvl",
							data: {mode:"paymentSummmary",maxSize:objData.listDoctorCode.length, hospitalCode:$("#hospitalCode").val(),date:$("#date").val(),count:i,doctorCode:objData.listDoctorCode[i].doctorCode},
							contentType: "application/json; charset=utf-8",
							dataType: "json",
							cache : false,
							success: function(data){
								document.getElementById("progress_payment").innerHTML = data.count+" / " + objData.listDoctorCode.length;
								if(data.lastData == "Y"){
									if(data.count>0){
							        	document.getElementById("img_payment_summary").src="../../images/succeed_icon.gif";
							        	//alert("data.count = " +data.count +" objData.listDoctorCode.length = "+objData.listDoctorCode.length);
							        	//alert(data.count == objData.listDoctorCode.length);
										if(data.count == objData.listDoctorCode.length){
											bankPayment();
										}else{
											document.getElementById("progress_payment").innerHTML = "0 / " + objData.listDoctorCode.length;
								        	document.getElementById("img_payment_summary").src="../../images/failed_icon.gif";
											document.getElementById("img_bank_payment").src="../../images/failed_icon.gif";
											document.getElementById("RUN").disabled = "";
											document.getElementById("STOP").disabled = "";
								        	alert("    Fail!!!  ");
										}
									}else{
										document.getElementById("progress_payment").innerHTML = "0 / " + objData.listDoctorCode.length;
							        	document.getElementById("img_payment_summary").src="../../images/failed_icon.gif";
										document.getElementById("img_bank_payment").src="../../images/failed_icon.gif";
										document.getElementById("RUN").disabled = "";
										document.getElementById("STOP").disabled = "";
							        	alert("    Fail!!!  ");
									}
								}
							},
							error: function(xhr,state,exception) {
								alert("exception paymentSummary => " + exception);
							}
						   });
					}
			}
			function bankPayment(){
				xhr = $.ajaxQueue({
					type: "GET",
					url: "../../ProcessAllMonthlySrvl",
					data: {mode:"bankPayment",date:$("#date").val(),hospitalCode : $("#hospitalCode").val(),
					recNo : "2",user : $("#user").val(),password:""},
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					async : false,
					cache : false,
					success: function(data){
			        	document.getElementById("img_bank_payment").src="../../images/processing_icon.gif";	
						if(data.count>0){
							document.getElementById("progress_bank").innerHTML = data.count+" / 1";
				        	document.getElementById("img_bank_payment").src="../../images/succeed_icon.gif";
							document.getElementById("RUN").disabled = "";
							document.getElementById("STOP").disabled = "";
				        	alert("    Succeed!!!  ");
						}else{
							document.getElementById("progress_bank").innerHTML = data.count+" / 1";
				        	document.getElementById("img_bank_payment").src="../../images/failed_icon.gif";
							document.getElementById("RUN").disabled = "";
							document.getElementById("STOP").disabled = "";
				        	alert("    Fail!!!  ");
						}
					},
					error: function(xhr,state,exception) {
						alert("exception bankPayment => " + exception);
					}
			    });
			}
	        $("#RUN").click(function () {
	        	document.getElementById("progress_monthly").innerHTML = "0/0";
	        	document.getElementById("progress_expense").innerHTML = "0/2";
	        	document.getElementById("progress_payment").innerHTML = "0/0";
	        	document.getElementById("progress_bank").innerHTML = "0/1";
				document.getElementById("img_monthly_calculate").src="../../images/waiting_icon.gif";
				document.getElementById("img_expense_calculate").src="../../images/waiting_icon.gif";
				document.getElementById("img_payment_summary").src="../../images/waiting_icon.gif";
				document.getElementById("img_bank_payment").src="../../images/waiting_icon.gif";
				document.getElementById("RUN").disabled = "disabled";
				document.getElementById("STOP").disabled = "disabled";
				monthlyCalulate();
	          });
	        
        });
        </script>
    </head>
    <body onload="getDoctorData()">
        <form id="mainForm" name="mainForm" method="post" action="ProcessAllMonthly.jsp">
        	<input type="hidden" name="hospitalCode" id="hospitalCode" value="<%=hospitalCode%>"/>
        	<input type="hidden" name="user" id="user" value="<%=user%>"/>
        	<center>
                <table width="800" border="0">
                    <tr><td align="left">
                        <b><font color='#003399'><%=Utils.getInfoPage("ProcessAllMonthly.jsp", labelMap.getFieldLangSuffix(), new DBConnection(""+session.getAttribute("HOSPITAL_CODE")))%></font></b>
                    </td></tr>
				</table>
            </center>
            <table class="form">
                <tr>
                    <th colspan="2">${labelMap.TITLE_MAIN}</th>
                </tr>  
                <tr align="center">
            		<td class="label" align="right" width="40%">Date</td>
                    <td class="input" valign="middle" width="60%" align="left">
                    	<input type="text" value="<%=startDateStr%>" id="date" name="date" class="short" value="<%=request.getParameter("START_DATE") == null ? "" : request.getParameter("START_DATE")%>" />
                        <input name="image2" type="image" class="image_button" onclick="displayDatePicker('date'); return false;" src="../../images/calendar_button.png" alt="" />
                    </td>
                </tr>
                <tr>
                    <th colspan="2" class="buttonBar">                        
<%--                    <input type="button" id="SELECT" name="SELECT" class="button" value="${labelMap.SELECT}" onclick="SELECT_Click()" />    --%>
                        <input type="button" id="RUN" name="RUN" class="button" value="${labelMap.RUN}"/>
                        <input type="button" id="STOP" name="STOP" class="button" value="Stop"/>
                        <input type="button" id="CLOSE" name="CLOSE" class="button" value="${labelMap.CLOSE}" onclick="window.location='../process/ProcessFlow.jsp'" />                    </th>
                </tr>
            </table>
            <hr />
            <table class="data" id="dataTable" name="dataTable">
                <tr>
                    <th colspan="3" class="alignLeft">
                        <div style="float: left;">${labelMap.TITLE_MAIN}</div>
                        <div style="float: right;" id="PROGRESS" name="PROGRESS"></div>
                    </th>
                </tr>
                <tr>
                    <td class="sub_head"><%=labelMap.get("COL_0")%></td>
                    <td class="sub_head"><%=labelMap.get("PROGRESS")%></td>
                    <td class="sub_head"><%=labelMap.get("COL_1")%></td>
                </tr>
                <tr>
                    <td class="row0 alignCenter">Monthly Calculate</td>
                    <td class="row0 alignCenter"><div id="progress_monthly" name="progress_monthly"></div></td>
                    <td class="row0 alignCenter"><img src="../../images/waiting_icon.gif" alt="" name="img_monthly_calculate" id="img_monthly_calculate"/></td>
                </tr>
                <tr>
                    <td class="row1 alignCenter">Expense Calculate</td>
                    <td class="row1 alignCenter"><div id="progress_expense" name="progress_expense"></div></td>
                    <td class="row1 alignCenter"><img src="../../images/waiting_icon.gif" alt="" name="img_expense_calculate" id="img_expense_calculate"/></td>
                </tr>
                <tr>
                    <td class="row0 alignCenter">Payment Summary</td>
                    <td class="row0 alignCenter"><div id="progress_payment" name="progress_payment"></div></td>
                    <td class="row0 alignCenter"><img src="../../images/waiting_icon.gif" alt="" name="img_payment_summary" id="img_payment_summary"/></td>
                </tr>
                <tr>
                    <td class="row1 alignCenter">Bank Payment</td>
                    <td class="row1 alignCenter"><div id="progress_bank" name="progress_bank"></div></td>
                    <td class="row1 alignCenter"><img src="../../images/waiting_icon.gif" alt="" name="img_bank_payment" id="img_bank_payment"/></td>
                </tr>
            </table>
        </form>
    </body>
</html>